package com.xiaomage.parcelabledemo;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable {
    public static String age="18";
    private String name;
    private transient String sex;

    public User(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    protected User(Parcel in) {
        //从序列化中创建对象：反序列化操作
        name = in.readString();
        sex=in.readString();
        age=in.readString();
    }

    /**
     * 将Parcelable容器内的对象转换成
     */
    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            //从序列化中创建对象：反序列化操作
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            //创建指定长度的原始对象数组
            return new User[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //将对象写入序列化中
        dest.writeString(name);
        dest.writeString(age);
        dest.writeString(sex);
    }
    @Override
    public int describeContents() {
        //几乎所有的情况都返回0，仅在当前对象存在文件描述符时返回1
        return 0;
    }





    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
